<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      :model="queryParams"
      ref="queryForm"
      size="small"
      inline
      label-width="68px"
    >
      <el-form-item label="品种" prop="variety">
        <el-select
          v-model="queryParams.variety"
          placeholder="请选择品种"
          filterable
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="item in dict.type.mc_medicine_variety_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="条码" prop="barCode">
        <el-input
          v-model="queryParams.barCode"
          placeholder="请输入条码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="药品名称" prop="medicineName">
        <el-input
          v-model="queryParams.medicineName"
          placeholder="请输入药品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['mc:medicine:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleEdit()"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['mc:medicine:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >
          批量删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="80" align="center" />
      <el-table-column label="药品名称" align="center" prop="medicineName" />
      <el-table-column label="品种" align="center">
        <template slot-scope="{ row }">
          <dict-tag :options="dict.type.mc_medicine_variety_type" :value="row.variety" />
        </template>
      </el-table-column>
      <el-table-column label="条码" align="center" prop="barCode" />
      <el-table-column label="商品分类" align="center">
        <template slot-scope="{ row }">
          <dict-tag :options="dict.type.mc_medicine_type" :value="row.medicineType" />
        </template>
      </el-table-column>
      <el-table-column label="新特药" align="center">
        <template slot-scope="{ row }">
          {{ row.newSpecial == 1 ? "是" : row.newSpecial == 0 ? "否" : "" }}
        </template>
      </el-table-column>
      <el-table-column label="药品规格" align="center" prop="medicineSpecifications" />
      <el-table-column label="处方分类" align="center">
        <template slot-scope="{ row }">
          <dict-tag
            :options="dict.type.mc_prescription_type"
            :value="row.prescriptionType"
          />
        </template>
      </el-table-column>
      <el-table-column label="生产厂家" align="center" prop="manufacturer" />
      <el-table-column label="包装规格" align="center" prop="packagingSpecifications" />
      <el-table-column label="操作" align="center" width="140">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['mc:medicine:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row.id)"
          >
            修改
          </el-button>
          <el-button
            v-hasPermi="['mc:medicine:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listMedicine, delMedicine } from "@/api/mc/medicine";

export default {
  name: "Medicine",
  dicts: [
    "mc_medicine_variety_type",
    "mc_medicine_type",
    "mc_prescription_type",
    "mc_medicine_unit",
    "mc_dosage_form",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 药品信息表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        variety: null,
        barCode: null,
        medicineName: null,
        medicineType: null,
      },
    };
  },
  created() {
    this.getList();
  },
  watch: {
    $route(val) {
      if (val.path == "/medicine/medicine" && val.query.t) {
        this.getList();
      }
    },
  },
  methods: {
    /** 查询药品信息列表 */
    getList() {
      this.loading = true;
      listMedicine(this.queryParams)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增/编辑操作 */
    handleEdit(id = "") {
      this.$router.push({ path: "/medicine/medicine-edit", query: { id } });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(`确认删除已选中的${row.id ? 1 : ids.length}条药品信息吗？`)
        .then(() => {
          delMedicine(ids).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          });
        })
        .catch(() => {});
    },
  },
};
</script>
